
package com.jf.cloud.user.controller.app;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PriceUtil;
import com.jf.cloud.user.constant.RechargeIoTypeEnum;
import com.jf.cloud.user.constant.RechargeTypeEnum;
import com.jf.cloud.user.dto.UserAdminDTO;
import com.jf.cloud.user.dto.UserBalanceRechargeOrderDTO;
import com.jf.cloud.user.model.UserBalanceLog;
import com.jf.cloud.user.model.UserExtension;
import com.jf.cloud.user.service.UserBalanceLogService;
import com.jf.cloud.user.service.UserExtensionService;
import com.jf.cloud.user.service.UserRechargeService;
import com.jf.cloud.user.vo.UserRechargeVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 余额充值级别表
 *
 * @author zz
 * @date 2021-04-27 15:51:36
 */
@RestController("appUserRechargeController")
@RequestMapping("/user_recharge")
@Tag(name = "app-余额充值")
public class UserRechargeController {

    @Autowired
    private UserRechargeService userRechargeService;

    @Autowired
    private UserExtensionService userExtensionService;
    @Autowired
    private UserBalanceLogService userBalanceLogService;

    @GetMapping("/list")
    @Operation(summary = "获取余额充值级别表列表" , description = "分页获取余额充值级别表列表")
    public ServerResponseEntity<List<UserRechargeVO>> list() {
        return ServerResponseEntity.success(userRechargeService.list());
    }

    @PostMapping("/recharge")
    @Operation(summary = "充值订单" , description = "生成余额充值订单，保存充值记录")
    public ServerResponseEntity<Long> save(@Valid @RequestBody UserBalanceRechargeOrderDTO userBalanceRechargeOrderDTO) {
        Long amount = PriceUtil.toLongCent(userBalanceRechargeOrderDTO.getAmount());
        Long rechargeId = userBalanceRechargeOrderDTO.getRechargeId();
        if (rechargeId != null) {
            UserRechargeVO userRecharge = userRechargeService.getRechargeInfo(rechargeId);
            amount = userRecharge.getRechargeAmount();
        }
        if (amount == null || amount < 1) {
            throw new LuckException("充值金额有误");
        }
        UserExtension userExtension = userExtensionService.getByUserId(AuthUserContext.get().getUserId());
        long userBalance = userExtension.getActualBalance() + amount;
        if (userBalance > Constant.MAX_USER_BALANCE) {
            throw new LuckException("此次充值会导致余额超出最大限制，故无法进行此操作,请减少充值金额后重试");
        }
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(AuthUserContext.get().getUserId());
        userBalanceLog.setChangeBalance(amount);
        userBalanceLog.setIoType(RechargeIoTypeEnum.INCOME.value());
        userBalanceLog.setIsPayed(0);
        userBalanceLog.setRechargeId(userBalanceRechargeOrderDTO.getRechargeId());
        userBalanceLog.setType(RechargeTypeEnum.RECHARGE.value());
        userBalanceLogService.save(userBalanceLog);
        return ServerResponseEntity.success(userBalanceLog.getBalanceLogId());
    }

    @PutMapping("/update_user_balance")
    @Operation(summary = "平台收款码修改会员余额" , description = "平台收款码修改会员余额")
    public ServerResponseEntity<Boolean> updateUserBalance(@RequestBody @Valid UserAdminDTO userAdminDTO) {
        if(userAdminDTO.getBalanceValue().doubleValue() >=0){
                throw new LuckException("金额有误,请重试");
        }
        userAdminDTO.getUserIds().add(AuthUserContext.get().getUserId());
        return ServerResponseEntity.success(userRechargeService.batchUpdateUserBalance(userAdminDTO));
    }
}
